package com.naiterui.ehp.bp.domain;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Description 就诊人
 * @Author gongbaoqiang
 * @Date 2020/7/9 10:28 AM
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@Entity
@Table(name = "patient_inquirer_info")
public class PatientInquirerInfo implements Serializable {
    private static final long serialVersionUID = 4636695318143851901L;

    /**
     * 认证状态：未认证
     */
    public static final int ACCOUNT_STATUS_NO = 0;
    /**
     * 已认证
     */
    public static final int ACCOUNT_STATUS_YES = 1;

    /**
     * 状态：1：正常，2：已删除
     */
    public static final int STATUS_DEFAULT = 1;
    public static final int STATUS_DELETE = 2;

    /**
     * 儿童标识：是
     */
    public static final int CHILD_TAG_YES = 1;
    /**
     * 儿童标识：否
     */
    public static final int CHILD_TAG_NO = 0;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 患者id
     */
    @Column(name = "patient_id")
    private Long patientId;

    /**
     * 就诊人与患者关系
     */
    @Column(name = "relation")
    private Integer relation;

    /**
     * 就诊人姓名
     */
    @Column(name = "name")
    private String name;

    /**
     * 就诊人电话
     */
    @Column(name = "phone")
    private String phone;

    /**
     * 就诊人性别
     */
    @Column(name = "gender")
    private Integer gender;

    /**
     * 身份证号
     */
    @Column(name = "id_card")
    private String idCard;
    /**
     * 出生日期
     */
    @Column(name = "birthday")
    private Date birthday;

    /**
     * 出生证明图片
     */
    @Column(name = "birth_image")
    private String birthImage;

    /**
     * 监护人姓名
     */
    @Column(name = "guardian_name")
    private String guardianName;

    /**
     * 监护人身份证号
     */
    @Column(name = "guardian_id_card")
    private String guardianIdCard;

    /**
     * 监护人电话
     */
    @Column(name = "guardian_phone")
    private String guardianPhone;

    /**
     * 状态：1：正常，2：已删除
     */
    @Column(name = "status")
    private Integer status;

    @Column(name = "created_at")
    private Date createdAt;

    @Column(name = "created_by")
    private String createdBy;

    @Column(name = "changed_by")
    private String changedBy;

    @Column(name = "changed_at")
    private Date changedAt;

    @Version
    private Integer version;

    /**
     * 城市信息
     */
    @Column(name = "city_id")
    private Integer cityId;
    /**
     * 居住地址
     */
    @Column(name = "address")
    private String address;
    /**
     * 紧急联系人姓名
     */
    @Column(name = "contact_name")
    private String contactName;
    /**
     * 紧急联系人电话
     */
    @Column(name = "contact_phone")
    private String contactPhone;
    /**
     * 婚姻状况：0未婚；1已婚
     */
    @Column(name = "marital_status")
    private Short maritalStatus;

    /**
     * 低龄儿童标识(0，否；1，是)
     */
    @Column(name = "child_tag")
    private Integer childTag;

    /**
     * 过敏史
     */
    @Column(name = "allergy")
    private String allergy;

    /**
     * 既往史
     */
    @Column(name = "past_history")
    private String pastHistory;

    /**
     * 认证状态：0：未认证，1：已认证
     */
    @Column(name = "account_status")
    private Integer accountStatus;

    /**
     * 根据出生日期计算年龄
     *
     *
     * @return
     */
    public String getAge() {
        Date birthday = this.birthday;
        String age = "";
        try {
            int year = 0;
            int month = 0;
            int day = 0;
            Calendar now = Calendar.getInstance();
            Calendar bc = Calendar.getInstance();
            bc.setTime(birthday);
            boolean sameDay = now.get(Calendar.YEAR) == bc.get(Calendar.YEAR)
                && now.get(Calendar.DAY_OF_YEAR) == bc.get(Calendar.DAY_OF_YEAR);
            if (sameDay) {
                return "1天";
            }
            if(now.after(bc)){
                int nowYear = now.get(Calendar.YEAR);
                int nowMonth = now.get(Calendar.MONTH);
                int nowDay = now.get(Calendar.DAY_OF_MONTH);

                int birthYear = bc.get(Calendar.YEAR);
                int birthMonth = bc.get(Calendar.MONTH);
                int birthDay = bc.get(Calendar.DAY_OF_MONTH);

                //计算整岁
                year = nowYear - birthYear;
                if (year == 1 && now.get(Calendar.MONTH) == bc.get(Calendar.MONTH)) {
                    if (now.get(Calendar.DAY_OF_MONTH) == bc.get(Calendar.DAY_OF_MONTH)) {
                        return "1岁";
                    } else if (now.get(Calendar.DAY_OF_MONTH) < bc.get(Calendar.DAY_OF_MONTH)) {
                        // 计算月份差
                        int monthDiff = nowMonth + 11 - birthMonth;
                        return monthDiff + "个月";
                    }
                }
                if(now.get(Calendar.DAY_OF_YEAR) < bc.get(Calendar.DAY_OF_YEAR)){
                    year --;
                }

                //计算月份差
                int monthDiff  = nowMonth - birthMonth;
                //跨年情况
                if(monthDiff < 0){
                    monthDiff = nowMonth + 12 - birthMonth;
                }
                //小于1岁时，返回XX月XX天
                if(year < 1){
                    int dayDiff = nowDay - birthDay;
                    //0个月的情况
                    if(monthDiff == 0){
                        day = dayDiff;
                    } else {
                        if(dayDiff < 0){
                            month = monthDiff - 1;
                            day = dayDiff + now.getActualMaximum(Calendar.DAY_OF_MONTH);
                        } else {
                            month = monthDiff;
                            day = dayDiff;
                        }
                    }
                    if(month >= 1){
                        age = month + "个月";
                    }
                    if (month < 1 && day > 0) {
                        age = age + day + "天";
                    }
//                }
//                else if(year < 3){
//                    age = year + "岁" + (monthDiff > 0 ? monthDiff + "个月" : "");
                } else {
                    age = year + "岁";
                }
            }

            /*if ("0岁".equals(age)) {
                age = "未知";
            }*/

            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return "未知";
        }
    }

    /**
     * 年龄：整岁
     * @return
     */
    public Integer getAgeYear(){
        Calendar now = Calendar.getInstance();
        Calendar bc = Calendar.getInstance();
        if (this.birthday == null) {
            return null;
        }
        bc.setTime(birthday);
        int age = 0;
        if(now.after(bc)) {
            int nowYear = now.get(Calendar.YEAR);
            int birthYear = bc.get(Calendar.YEAR);
            //计算整岁
            int year = nowYear - birthYear;
            if (now.get(Calendar.DAY_OF_YEAR) < bc.get(Calendar.DAY_OF_YEAR)) {
                year--;
            }
            age = year;
        }
        return age;
    }

    /**
     * 判断就诊人是否小于18岁
     * @return
     */
    public boolean ageLessThan18(){
        Date birthday = this.birthday;
        Calendar now = Calendar.getInstance();
        int nowYear = now.get(Calendar.YEAR);
        Calendar bc = Calendar.getInstance();
        bc.setTime(birthday);
        int birthYear = bc.get(Calendar.YEAR);
        //计算整岁
        int year = nowYear - birthYear;
        if(now.get(Calendar.DAY_OF_YEAR) < bc.get(Calendar.DAY_OF_YEAR)){
            year --;
        }
        if(year < 15){
            return true;
        }
        return false;
    }
}
